package com.example.tool.widget.mpview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.example.tool.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;


/**
 * Created by asus
 *
 * @author:asus
 * @desciption:${TODO}
 * @date:2018/8/17
 */

public class CountLineChart extends LineChart {

    private Bitmap bitmap;
    private Paint paint;

    public CountLineChart(Context context) {
        super(context);
    }

    public CountLineChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CountLineChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void drawMarkers(Canvas canvas) {
        // if there is no marker view or drawing marker is disabled
        if (mMarker == null || !isDrawMarkersEnabled() || !valuesToHighlight()) {
            Log.e("DebugLog", " pos[0] -------------- ");
            return;
        }


        for (int i = 0; i < mIndicesToHighlight.length; i++) {

            Highlight highlight = mIndicesToHighlight[i];

            IDataSet set = mData.getDataSetByIndex(highlight.getDataSetIndex());

            Entry e = mData.getEntryForHighlight(mIndicesToHighlight[i]);
            int entryIndex = set.getEntryIndex(e);
            // make sure entry not null
            if (e == null || entryIndex > set.getEntryCount() * mAnimator.getPhaseX())
                continue;

            float[] pos = getMarkerPosition(highlight);



            // check bounds这句是检查边界的，必须写在paint画圆之上，否则圆过边界不消失
            if (!mViewPortHandler.isInBounds(pos[0], pos[1])) {

                continue;
            }

            // 画图片，就是贴图
            canvas.drawBitmap(bitmap, pos[0] - bitmap.getWidth() / 2, pos[1] - bitmap.getHeight() / 2, paint);

            // callbacks to update the content
            mMarker.refreshContent(e, highlight);

            if(highlight.getX()==28f||highlight.getX()==29f){
//            Log.e("DebugLog", " &&&&&&&&&&&&&&&&&&&&&&& " );
                //倒数1,2位，需要调节位置
            mMarker.draw(canvas, pos[0]-55, pos[1] - 20);
             return;
            }
            // draw the marker 源码屏蔽点
            mMarker.draw(canvas, pos[0], pos[1] - 20);

            /*end*/
            /*修改点：将mark放置高亮线的顶部*/
//            mMarker.draw(canvas,pos[0],mViewPortHandler.contentTop());
            /*end*/
        }
    }

    @Override
    protected void init() {
        super.init();
        mRenderer = new CountLineChartRender(this, mAnimator, mViewPortHandler, 4);
        bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.mark_dot);
        //自定义部分，在高亮时画环形，表示选中
        paint = new Paint();

        paint.setStyle(Paint.Style.STROKE);//画笔为填充模式
        paint.setAntiAlias(true);//消除锯齿，使更光滑
        paint.setStrokeWidth(1);
    }

    PointF downPoint = new PointF();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);// 用getParent去请求,
        // 不拦截
        return super.dispatchTouchEvent(ev);
    }

    //为解决和viewpager滑动冲突，将viewpager滑动范围缩小
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downPoint.x = event.getX();
                downPoint.y = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //通知父控件不要拦截我自己的事件
                if (getScaleX() > 1 && Math.abs(event.getX() - downPoint.x) > 5) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return super.onTouchEvent(event);
    }


}
